/*
	Function is used to check if the year is a leap year
*/
function IsLeapYear(theyear) 
{
	if (theyear % 100 == 0) 
	{
    	if (theyear % 400 == 0) 
	  	{ 
	  		return true; 
	  	}
	}
    else 
  	{
    	if ((theyear % 4) == 0) 
	 	{ 
	 		return true; 
	 	}
     }

	return false;
}

/*
	Function is used to check if a series of select boxes used to create a date
	contain a valid date (i.e. not February 30)
*/
function CheckDate(theform,thefield,themessage,forcefuture)
{
	var theday_field_name=thefield+"_day";
	var themonth_field_name=thefield+"_month";
	var theyear_field_name=thefield+"_year";
	var theday=0;
	var themonth=0;
	var theyear=0;
	var current_date=new Date()
	var current_year=current_date.getFullYear();
	var current_day=current_date.getDate();
	var current_month=current_date.getMonth()+1;
	
	theday=theform[theday_field_name][theform[theday_field_name].selectedIndex].value;
	themonth=theform[themonth_field_name][theform[themonth_field_name].selectedIndex].value;
	theyear=theform[theyear_field_name][theform[theyear_field_name].selectedIndex].value;
	
	if ((theday > 0) && (themonth > 0) && (theyear > 0))
	{
		if (themonth ==  2) 
		{
			if (theday > 28)
			{
				if (IsLeapYear(theyear))
				{
					if (theday > 29)
					{
						alert("February only has 29 days in a leap year.");
						theform[theday_field_name].focus();
						return false;
					}
				}
				else
				{
					alert("February only has 28 days.");	
					theform[theday_field_name].focus();
					return false;
				}
			}
		}
		if ((themonth == 4) || (themonth == 6) || (themonth == 9) || (themonth == 11))
		{
			if (theday > 30)
			{
				alert("Please pick 30 or less days for the " + themessage + ".");
				theform[theday_field_name].focus();
				return false;
			}
		}
	}
	else
	{
		alert("Please select a valid date.");
		theform[theday_field_name].focus();
		return false;
	}
	
	//if we are forcing a date in the future
	if (forcefuture == 1)
	{
		//lets make sure the date is in the future
		//previous year
		if (theyear < current_year)
		{
			theform[theday_field_name].focus();
			alert("Please select a date in the future.");
			return false;
		}
		
		//current year, previous month or previously in this month
		if (((theyear == current_year) && (themonth < current_month)) || ((theyear == current_year) && (themonth == current_month) && (theday < current_day)))
		{
			theform[theday_field_name].focus();
			alert("Please select a date in the future.");
			return false;
		}
		
		//ensure the date is less then one year in the future for dining
		if (thefield == 'dining')
		{
			//compare the dates 
			//if its next year
			if (theyear > current_year)
			{
				//if the month is greater
				if (themonth > current_month)
				{
					alert("Please select a date within 12 months of today.");
					theform[theday_field_name].focus();
					return false;
				}
				
				//if the month is the same and day is greater
				if ((themonth == current_month) && (theday > current_day))
				{
					alert("Please select a date within 12 months of today.");
					theform[theday_field_name].focus();
					return false;
				}
			}
		}
	}
	
	return true;
}
